package com.lijinjiang;

import com.lijinjiang.component.JDatePanel;
import com.lijinjiang.constant.Icons;
import com.lijinjiang.mode.PickerMode;
import com.lijinjiang.model.DateModel;
import com.lijinjiang.util.TextFieldFormatter;

import javax.swing.*;
import java.awt.*;
import java.time.LocalDateTime;

/**
 * @Description 日期选择组件
 * @Author 03010430
 * @Date 2024-10-29 15:30
 */
public class JDatePicker extends JComponent {
    private Popup popup;//弹出页面
    private final JDatePanel datePanel;//选择日期面板
    private final JFormattedTextField formattedTextField;
    private final JButton button;


    public JDatePicker() {
        this(PickerMode.DAY);
    }

    public JDatePicker(PickerMode pickerMode) {
        this(new JDatePanel(pickerMode));
    }

    public JDatePicker(PickerMode pickerMode, Object value) {
        this(new JDatePanel(pickerMode, value));
    }

    public JDatePicker(JDatePanel datePanel) {
        this.datePanel = datePanel;
        this.setLayout(new BorderLayout());
        JPanel contentPanel = new JPanel();
        formattedTextField = new JFormattedTextField(new TextFieldFormatter(datePanel.getPickerMode()));
        DateModel<?> model = datePanel.getDateModel();
        setTextFieldValue(formattedTextField, model);
        formattedTextField.setEditable(false);
        formattedTextField.setPreferredSize(new Dimension(215, 30));
        contentPanel.add(formattedTextField);

        button = new JButton();
        button.setFocusable(true);
        button.setIcon(Icons.getCalendarIcon());
        button.setPreferredSize(new Dimension(30, 30));
        button.setMargin(new Insets(0, 0, 0, 0));//将边框外的上下左右空间设置为0
        button.setIconTextGap(0);//将标签中显示的文本和图标之间的间隔量设置为0
        button.setBorderPainted(false);//不打印边框
        button.setBorder(null);//除去边框
        button.setText(null);//除去按钮的默认名称
        button.setFocusPainted(false);//除去焦点的框
        button.setContentAreaFilled(false);//除去默认的背景填充
        button.addActionListener(e -> {
            if (popup == null) {
                showPopup();
            } else {
                hidePopup();
            }
        });

        contentPanel.add(button);
        this.add(contentPanel, BorderLayout.CENTER);


        datePanel.getDateModel().addChangeListener(e -> {
            if (datePanel.getDateModel().isSelected()) {
                hidePopup();
                setTextFieldValue(formattedTextField, datePanel.getDateModel());
                datePanel.getDateModel().setSelected(false);
            }
        });
    }

    private void showPopup() {
        if (popup == null) {
            datePanel.setVisible(true);//显示选择日期面板
            datePanel.repaint();
            PopupFactory factory = new PopupFactory();
            Point point = button.getLocationOnScreen();//按钮位置
            int y = point.y + button.getHeight() + 1;
            int x = point.x + button.getWidth() - datePanel.getWidth();
            popup = factory.getPopup(this, datePanel, x, y);
            popup.show();
        }
    }

    private void hidePopup() {
        if (popup != null) {
            popup.hide();
            popup = null;
        }
    }

    private void setTextFieldValue(JFormattedTextField textField, DateModel<?> model) {
        if (model != null) {
            boolean selected = model.isSelected();
            if (!selected) {
                textField.setValue(null);
            } else {
                LocalDateTime time = LocalDateTime.of(model.getYear(), model.getMonth(), model.getDay(), model.getHour(), model.getMinute(), model.getSecond());
                textField.setValue(time);
            }
        }
    }

    public String getValue() {
        return formattedTextField.getText();
    }
}
